﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Common.Activities.Contract;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace DWF.Studio.RemoteCtrls.ActivityToolBox.Node
{
    internal sealed class ActivityNode : TreeNode, System.ICloneable
    {
        private static readonly Uri _defaultIcon = new Uri("..\\Resources\\ToolBox\\default.png", UriKind.Relative);

        public ActivityNode()
        {
            IsExisting = true;
            Visible = true;
            IsLeaf = true;
        }

        public ActivityNode(Type activityType)
            : this()
        {
            ActivityType = activityType;

            init();
        }

        private void init()
        {
            var activityAttribute = ActivityType.GetCustomAttribute<ToolBoxActivityAttribute>(false);
            if (activityAttribute != null)
            {
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = activityAttribute.IconUri;
                bitmapImage.EndInit();
                this.Icon = bitmapImage;

                this.SortNo = activityAttribute.ActivitySort;
                this.DisplayName = activityAttribute.DisplayName;
                this.ToolTip = activityAttribute.ToolTip;
            }
            else
            {
                this.DisplayName = getFriendlyTypeName(ActivityType);
                var toolboxBitmapAttribute = ActivityType.GetCustomAttribute<ToolboxBitmapAttribute>(false);

                if (toolboxBitmapAttribute != null)
                {
                    var bitmap = toolboxBitmapAttribute.GetImage(ActivityType, large: false);
                    if (bitmap != null)
                    {
                        using (MemoryStream memory = new MemoryStream())
                        {
                            bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Tiff);
                            memory.Position = 0;
                            BitmapImage bitmapImage = new BitmapImage();
                            bitmapImage.BeginInit();
                            bitmapImage.StreamSource = memory;
                            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                            bitmapImage.EndInit();

                            this.Icon = bitmapImage;
                        }
                    }
                }
                else
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();
                    bitmapImage.UriSource = _defaultIcon;
                    bitmapImage.EndInit();
                    this.Icon = bitmapImage;
                }
            }
        }

        public Type ActivityType
        {
            private set;
            get;
        }

        public override ObservableCollection<TreeNode> Children
        {
            get
            {
                return null;
            }
        }

        public override ObservableCollection<TreeNode> SortChildren
        {
            get
            {
                return null;
            }
        }

        private string getFriendlyTypeName(Type activityType)
        {
            string displayName = String.Empty;

            var nameAttri = activityType.GetCustomAttribute<DisplayNameAttribute>(false);
            if (nameAttri != null)
            {
                displayName = nameAttri.DisplayName;
            }

            if (String.IsNullOrWhiteSpace(displayName))
            {
                displayName = getFriendlyTypeName(activityType.Name);
            }

            return displayName;
        }

        /// <summary>
        /// 获取类型名称
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        private string getFriendlyTypeName(string typeName)
        {
            var index = typeName.IndexOf('`');
            if (index > 0)
            {
                String name = typeName.Substring(0, index);
                int count = int.Parse(typeName.Substring(index + 1, 1));

                name += "<";
                for (int i = 0; i < count; i++)
                {
                    name = name + "T, ";
                }
                name = name.TrimEnd(',', ' ') + ">";

                return name;
            }

            return typeName;
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public override void Sort()
        {
            throw new NotImplementedException();
        }

        public string GetActivityTypeId()
        {
            var fullName = this.ActivityType.FullName;
            var assemblyName = this.ActivityType.Assembly.FullName;

            return $"{fullName},{assemblyName}";
        }
    }
}
